package com.fjw.servlet;

import com.fjw.entity.User;
import com.fjw.service.UserService;
import com.fjw.service.impl.UserServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "userServlet",urlPatterns = "/userServlet")
public class UserServlet  extends HttpServlet {
    private UserService userService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        userService = new UserServiceImpl();
    }

    //req 表示请求对象
    //resp 表示响应对象
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //解决乱码
        req.setCharacterEncoding("UTF-8");
        //设置响应编码格式
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //获取输出流
        PrintWriter pw = resp.getWriter();
        //获取前端传递的参数
        String uname = req.getParameter("uname");
        String upwd = req.getParameter("upwd");

        //调用service方法 验证是否登录成功
        User user = userService.selectByUnameAndUpwd(uname, upwd);

        //给前端响应
        if(user ==null){
            //System.out.println("登录失败");
            pw.write("成功");
        }else {
            //System.out.println("登录成功");
            pw.write("失败");
        }


    }
}
